package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/4/26
 * Time: 13:06
 * To change this template use File | Settings | File Templates.
 */
public class WsdtResolver {
    public static WsdtEntity resolve(String wsdtFullPath, String wsdtStr){
        WsdtEntity wsdtEntity=null;
        File file=new File(wsdtFullPath);
        if(file.exists()){
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
                String line=null;
                wsdtEntity=new WsdtEntity();
                String entityName=wsdtFullPath.substring(wsdtFullPath.lastIndexOf("/")+1,wsdtFullPath.lastIndexOf(".wsdt"));
                wsdtEntity.setEntityName(entityName);
                String entityPackageName=wsdtStr.substring(0,wsdtStr.lastIndexOf("."));
                wsdtEntity.setEntityPackageName(entityPackageName);
                List<WsdtField> wsdtFieldList=new ArrayList<WsdtField>();
                wsdtEntity.setFieldList(wsdtFieldList);
                while ((line=reader.readLine())!=null){
                    line=line.trim();
                    if("".equals(line)||line.startsWith("#")){
                        continue;
                    }
                    if(line.startsWith("table:")){//表名
                        line=line.substring("table:".length());
                        String[] words=line.split("-");
                        if(words.length>=3){
                            wsdtEntity.setEntityName(words[2]);
                        }
                        if(words.length>=2){
                            wsdtEntity.setDescription(words[1]);
                        }
                        wsdtEntity.setTableName(words[0]);
                    }else{//字段信息
                        String[] words=line.split("-");
                        if(words.length>=4){
                            WsdtField wsdtField=new WsdtField(words[0],words[1],words[2],words[3]);
                            if(words.length>=5){
                                wsdtField.setPrimary(true);
                            }
                            if(words.length>=6){
                                wsdtField.setIncrement(true);
                            }
                            wsdtFieldList.add(wsdtField);
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                        System.out.println("finally resolve");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return wsdtEntity;
    }
}
